package com.hrh.hrhaiagent.tool;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import com.hrh.hrhaiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import java.io.File;

public class FileOperationTool {
    private final String FILE_PATH = FileConstant.FILE_SAVE_DIR + "/file";

    @Tool(description = "读取文件内容")
    public String readFile(@ToolParam(description = "文件名") String fileName){
        String filePath = FILE_PATH + "/" + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (IORuntimeException e) {
            throw new RuntimeException(e);
        }
    }

    @Tool(description = "写入文件内容")
    public String writeFile(@ToolParam(description = "文件名") String filename, @ToolParam(description = "文件内容") String content){
        String filePath = FILE_PATH + "/" + filename;
        try {
            FileUtil.mkdir(FILE_PATH);
            FileUtil.writeUtf8String(content, filePath);
            return "文件写入成功" + filePath;
        } catch (Exception e) {
            return "文件写入失败" + e.getMessage();
        }
    }

}
